﻿#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
using std::cout;
using std::endl;

inline static void* SystemAlloc(size_t kpage)
{
	
#ifdef _WIN32
		void* ptr = VirtualAlloc(0, kpage * (1 << 12), MEM_COMMIT | MEM_RESERVE,PAGE_READWRITE);
#else
		// linux下brk mmap等
#endif
	if (ptr == nullptr)
		throw std::bad_alloc();
	return ptr;
}
template<class T>
class ObjectPool
{
public:
	T* New()
	{
		T* obj = nullptr;
		// 如果⾃由链表有对象，直接取⼀个
		if (_freeList)
		{
			obj = (T*)_freeList;
			_freeList = *((void**)_freeList);
		}
		else
		{
			if (_leftBytes < sizeof(T))
			{
				_leftBytes = 128 * 1024;
				//_memory = (char*)malloc(_leftBytes);
				_memory = (char*)SystemAlloc(_leftBytes >> 10);
				if (_memory == nullptr)
				{
					throw std::bad_alloc();
				}
				//const size_t pages = 32; // 128KB / 4KB
				//_memory = (char*)SystemAlloc(pages);
				//_leftBytes = pages * (1 << 12); // 转换为字节数
				//if (_memory == nullptr)
				//{
				//	throw std::bad_alloc();
				//}
			}
			obj = (T*)_memory;
			size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);
			_memory += objSize;
			_leftBytes -= objSize;
		}
		// 使⽤定位new调⽤T的构造函数初始化
		new(obj)T;
		return obj;
	}
	void Delete(T* obj)
	{
		// 显⽰调⽤的T的析构函数进⾏清理
		obj->~T();
		// 头插到freeList
		*((void**)obj) = _freeList;
		_freeList = obj;
	}
private:
	char* _memory = nullptr; // 指向内存块的指针
	int _leftBytes = 0; // 内存块中剩余字节数
	void* _freeList = nullptr; // 管理还回来的内存对象的⾃由链表
};
struct TreeNode
{
	int _val;
	TreeNode* _left;
	TreeNode* _right;
	TreeNode()
		:_val(0)
		, _left(nullptr)
		, _right(nullptr)
	{}
};
void TestObjectPool()
{
	// 申请释放的轮次
	const size_t Rounds = 3;
	// 每轮申请释放多少次
	const size_t N = 100000;
	size_t begin1 = clock();
	std::vector<TreeNode*> v1;
	v1.reserve(N);
	for (size_t j = 0; j < Rounds; ++j)
	{
		for (int i = 0; i < N; ++i)
		{
		v1.push_back(new TreeNode);
		}
		for (int i = 0; i < N; ++i)
		{
			delete v1[i];
		}
		v1.clear();
	}
	size_t end1 = clock();
	ObjectPool<TreeNode> TNPool;
	size_t begin2 = clock();
	std::vector<TreeNode*> v2;
	v2.reserve(N);
	for (size_t j = 0; j < Rounds; ++j)
	{
		for (int i = 0; i < N; ++i)
		{
			v2.push_back(TNPool.New());
		}
		for (int i = 0; i < N; ++i)
		{
			TNPool.Delete(v2[i]);
		}
		v2.clear();
	}
	size_t end2 = clock();
	cout << "new cost time:" << end1 - begin1 << endl;
	cout << "object pool cost time:" << end2 - begin2 << endl;
}


//定长内存池
//template<size_t N>
//class ObjectPool
//{
//};

//template<class T>
//class ObjectPool
//{
//public:
//	ObjectPool()
//		:_memory(nullptr)
//		,_freelist(nullptr)
//		,_remainBytes(0)
//	{}
//	T* New()
//	{
//		T* obj = nullptr;
//		//如果_freelist不为空，把还回来的内存块重复利用
//		if (_freelist)
//		{
//			void* next = *(void**)_freelist;
//			obj = _freelist;
//			_freelist = next;
//			return obj;
//		}
//		else
//		{
//			//剩余内存不够一个对象大小时，则重新开大块空间
//			if (_remainBytes < sizeof(T))
//			{
//				_remainBytes = 128 * 1024;
//				_memory = (char*)malloc(_remainBytes);
//				if (_memory == nullptr)
//				{
//					throw std::bad_alloc();
//				}
//			}
//			obj = (T*)_memory;
//			size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);
//			_memory += objSize;
//			_remainBytes -= objSize;
//			//定位new，显示调用T的构造函数初始化
//			return obj;
//		}
//	}
//	void Delete(T* obj)
//	{
//		//显示调用析构函数清理对象
//		obj->~T();
//
//		*(void**)obj = _freelist;
//		_freelist = obj;
//	}
//	~ObjectPool() 
//	{
//		while (_memory) 
//		{
//			void* next = *(void**)_memory;
//			free(_memory);
//			_memory = (char*)next;
//		}
//		_freelist = nullptr;
//	}
//private:
//	char* _memory; //指向大块内存的指针
//	size_t _remainBytes; //大块内存切分过程中剩余字节数
//	void* _freelist; //还回来过程中链接的自由链表的头指针
//}; 

